<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var num=1;
    //自己调用自己
        function fn(){
            console.log("递归函数");
            if(num==3){
                return;
            }
            num++;
            fn();
        }
        fn();
    //求阶乘
        function fj(n){
            if(n==1){
                return 1;
            }
            return n*fj(n-1);
        }
        console.log("阶乘为"+fj(3));
    //求斐波那契数列
        function fq(n){
            if(n==1 || n==2){//第一个和第二个都是1
                return 1;
            }
            return fq(n-2)+fq(n-1);//等于前两项之和
        }
        console.log("斐波那契数为"+fq(6));//是8
    //遍历数据
        var data=[{
            id:1,
            name:'手机',
            goods:[{
                id:11,
                gname:'oppo',
                goods:[{
                    id:111,
                    gname:'oppoa10'
                },]
            },{
                id:12,
                gname:'华为'
            }]
        },{
            id:2,
            name:'电脑'
        }];
        //输入id号返回数据
            function getId(json,id){
                var o={};
                json.forEach(function(value){
                    if(value.id==id){
                        //console.log(value);
                        o=value;
                        //return value;
                    }else if(value.goods && value.goods.length>0){
                        o=getId(value.goods,id);
                    }                    
                });
                return o;
            }
            console.log(getId(data,1));
            console.log(getId(data,12));
            console.log(getId(data,111));
    </script>
</body>
</html>